//
//  NSString+Extend.m
//  WOM
//
//  Created by leeqb on 6/20/14.
//  Copyright (c) 2014 cst. All rights reserved.
//

#import "NSString+Extend.h"

@implementation NSString (Extend)

+ (NSString *)trimToEmpty:(NSString *)src
{
    if([src isKindOfClass:[NSString class]]) {
        if(src && ![src isEqual:[NSNull null]] && ![src isEqualToString:@""]) {
            return src;
        }
        
        return @"";
    } else {
        return 0;
    }
}

+ (BOOL)stringEquals:(NSString*)str1 to:(NSString*)str2
{
    if ( str1 == nil || str2 == nil ) {
        return NO;
    }
    return [str1 compare:str2 options:NSCaseInsensitiveSearch] == NSOrderedSame;
}

+ (BOOL)caseEquals:(NSString*)str1 to:(NSString*)str2
{
    return (str1 == nil || str2 == nil) ? NO : [str1 isEqualToString:str2];
}

+ (BOOL)startWith:(NSString*)prefix forString:(NSString*)text;
{
    if ( text != nil && prefix != nil ){
        if ( [prefix length] > [text length] ) {
            return NO;
        }
        
        NSString* prestr = [text substringToIndex:[prefix length]];
        if ( [self stringEquals:prestr to:prefix] ) {
            return YES;
        }
    }
    return NO;
}

+ (BOOL)endWith:(NSString*)suffix forString:(NSString*)text
{
    if ( text != nil && suffix != nil ){
        if ( [suffix length] > [text length] ) {
            return NO;
        }
        NSInteger len = [text length] - [suffix length];
        NSString* sufstr = [text substringFromIndex:len];
        if ( [self caseEquals:sufstr to:suffix] ) {
            return YES;
        }
    }
    return NO;
}

+ (NSString *)formatString:(id)src
{
    NSString *tempString = @"";
    if(src && ![src isEqual:[NSNull null]]) {
        if([src isKindOfClass:[NSString class]]) {
            tempString = [src stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        } else if([src isKindOfClass:[NSNumber class]]) {
            tempString = [NSString stringWithFormat:@"%@", src];
        }
    }
    tempString = [tempString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    /*
    // 如果为空，返回无
    if([tempString isEqualToString:@""]) {
        return LocalStringCommon(@"STRING_NONE");
    }
    */
    return tempString;
}

- (NSString *)URLEncodedString{
    NSString *result = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                           (CFStringRef)self,
                                                                           NULL,
                                                                           CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                           kCFStringEncodingUTF8));
    return result;
}

- (NSString*)URLDecodedString{
    NSString *result = (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
                                                                                           (CFStringRef)self,
                                                                                           CFSTR(""),
                                                                                           kCFStringEncodingUTF8));
    return result;
}

@end
